package accelerometer.example;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class AccelerometerExampleActivity extends Activity implements SensorEventListener {
	
	TextView acelerometro;
	SensorManager mSensorManager;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
    	acelerometro = (TextView) findViewById(R.id.acelerometro);
    	mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	mSensorManager.registerListener(this,
    						mSensorManager.getDefaultSensor
    						(Sensor.TYPE_ACCELEROMETER),
    						SensorManager.SENSOR_DELAY_NORMAL
    						);
    }
    
    @Override
    protected void onStop() {
    	mSensorManager.unregisterListener(this,
    					mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
    	);
    	super.onStop();
    }


	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		synchronized (this) {
			String txt = "\n\nSensor: ";
			Log.d("sensor", event.sensor.getName());
			
			switch (event.sensor.getType()) {
				
				case Sensor.TYPE_ACCELEROMETER:
					txt += "acelerometro\n";
					txt += "\n x: " + event.values[0];
					txt += "\n y: " + event.values[1];
					txt += "\n z: " + event.values[2];
					acelerometro.setText(txt);
					break;
			}
		}
		
	}
}